import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";

// 配置根路径
const BASE_URL = 'http://localhost:10900';
//window里配置全局内容,可以在任意script标签中使用
window.BASE_URL = BASE_URL;

//设置axios携带cookie信息
axios.defaults.withCredentials = true;

const app = createApp(App)
//给app也就是vue实例配置全局根路径,方便在template中使用
app.config.globalProperties.BASE_URL = BASE_URL;

//遍历ElementPlusIconsVue对象中的所有图标组件并注册到app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//app.use(ElementPlus) 使用Element-plus组件库
//app.use(ElementPlus,{ locale: zhCn })将El本地化为中文
app.use(ElementPlus,{ locale: zhCn }).use(store).use(router).mount('#app')

//解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}
axios.interceptors.response.use(
    function (response) {
        return response;
    },
    function (error) {
        console.log(error.response)
        if (error.response) {
            // 请求已发出，但服务器响应的状态码不在2xx范围内
            const { status } = error.response;

            if (status === 401) {
                // 处理401状态码
                alert("请先登录");
                window.location.href = '/custLogin';
            } else if (status === 402) {
                // 处理402状态码
                alert("请先注册");
                window.location.href = '/empLogin';
            }
        } else if (error.request) {
            // 请求已发出，但没有收到响应
            console.error('No response received:', error.request);
        } else {
            // 发生了一些设置请求时的问题
            console.error('Error', error.message);
        }
        return Promise.reject(error);
    }
)
window.getCust = async (currentCust) =>{
    await axios.get(BASE_URL+'/v1/customer/currentCustomer')
        .then(async (response)=>{
            if (response.data.code==2000){
               if (response.data.data!=null){
                   await axios.get(BASE_URL+'/v1/customer/select/'+response.data.data.username)
                       .then((response)=>{
                           if (response.data.code==2000){
                               currentCust.value = response.data.data;
                               console.log(currentCust.value)
                           }else {
                               ElMessage.error(response.data.msg)
                           }
                       })
               }
            }else {
                ElMessage.error(response.data.msg)
            }
        })
}
window.getEmp = async (currentEmp) =>{
    await axios.get(BASE_URL+'/v1/employee/currentEmployee')
        .then(async (response) => {
            if (response.data.code === 2000) {
                if (response.data.data != null){
                    await axios.get(BASE_URL+'/v1/employee/select/'+response.data.data.username)
                        .then((response)=>{
                            if (response.data.code==2000){
                                currentEmp.value = response.data.data;
                            }else {
                                ElMessage.error(response.data.msg)
                            }
                        })
                }
            } else {
                ElMessage.error(response.data.msg);
            }
        })
}
//向后端发请求获取字典项
window.loadDictOptions = async (object,dictCode)=>{
    let dictId;
    await axios.post(BASE_URL+'/v1/dict/select',{code:dictCode}).then((response)=>{
        if(response.data.code==2000){
            dictId = response.data.data[0].id;
        }else{
            ElMessage.error(response.data.msg);
        }
    });
    await axios.post(BASE_URL+'/v1/dictOption/select',{dictId:dictId}).then((response)=>{
        if(response.data.code==2000){
            object.value = response.data.data;
        }else{
            ElMessage.error(response.data.msg);
        }
    });
}
